
#!/usr/bin/python3
# -*- coding: utf-8 -*-
'''
# @file   : aqi.py
# @author : weichy
# @date   : 2019/01/08
# @version: 4.0
# @desc   :  1.0AQI计算
            2.0 读取json中api前5的数据
            3.0 csv文件互换
            4.0判断是什么格式的文件，并自动操作。
            5.0网络爬虫

'''
import requests
def get_html_text(url):
    #返回url文本
    url_text=requests.get(url,timeout=30)
    print(url_text.status_code)

    # aqi_div = '''<div class="span12 data">
    #     <div class="span1">
    #       <div class="value">
            
    # '''
    # index = url_text.text.find(aqi_div)
    # print(index)
    # begin_index = index + len(aqi_div)
    # end_index = begin_index +20
    # aqi_val = url_text.text[begin_index: end_index]
    # print(aqi_val)
    # return aqi_val
    return url_text.text


def main():
    city_pinyin=input('请输入城市拼音：')
    url ='http://www.pm25.in/' + city_pinyin
    url_text = get_html_text(url)
    aqi_div = '''<div class="span12 data">
        <div class="span1">
          <div class="value">
    '''
    index = url_text.find(aqi_div)
    print(index)
    begin_index = index + len(aqi_div)
    end_index = begin_index +10
    aqi_val = url_text[begin_index: end_index]
    print(aqi_val)

    print('{}空气质量为：{}'.format(city_pinyin,aqi_val))

if  __name__ == '__main__':
    main()